package com.yungu.swift.admin.route.helper;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.driver.model.dto.CarDto;
import com.yungu.swift.assets.driver.model.dto.CarModelDto;
import com.yungu.swift.assets.driver.model.dto.DriverDto;
import com.yungu.swift.assets.driver.service.CarModelService;
import com.yungu.swift.assets.driver.service.CarService;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.DriverConstant;
import com.yungu.swift.system.sys.model.dto.SysBusinessDto;
import com.yungu.swift.system.sys.model.dto.SysCityDto;
import com.yungu.swift.system.sys.model.dto.SysCompanyDto;
import com.yungu.swift.system.sys.model.dto.SysRegionalAgentDto;
import com.yungu.swift.system.sys.service.SysBusinessOrganizationService;
import com.yungu.swift.system.sys.service.SysCityService;
import com.yungu.swift.system.sys.service.SysCompanyService;
import com.yungu.swift.system.sys.service.SysRegionalAgentService;
import com.yungu.swift.utils.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Zerrion
 * @version 1.0
 * @description 查询数据的中间件(用于通过微服务获取数据)
 * @date 2017/10/28 13:49
 */
@Component
public class DataMiddleware {

    @Reference
    private CarModelService carModelService;
    @Reference
    private SysRegionalAgentService sysRegionalAgentService;
    @Reference
    private SysBusinessOrganizationService sysBusinessOrganizationService;
    @Reference
    private SysCompanyService sysCompanyService;
    @Reference
    private CarService carService;
    @Reference
    private DriverService driverService;
    @Reference
    private SysCityService sysCityService;

    /**
     * 获取车辆级别类型对应的map 用于导入车辆时对比
     *
     * @return
     */
    public final Map<String, CarModelDto> getCarLevelName2BeanMap() {
        Map carModelName2BeanMap = new HashMap();
        List<CarModelDto> carLevelDtoList = carModelService.list(null).getData();
        if (CollectionUtils.isNotEmpty(carLevelDtoList)) {
            for (CarModelDto carLevelDto : carLevelDtoList) {
                carModelName2BeanMap.put(carLevelDto.getBusinessType() + carLevelDto.getModelName(), carLevelDto);
            }
        }
        return carModelName2BeanMap;
    }

    /**
     * 获取代理的集合信息 map对应的键值为 name:sysRegionalAgentDto
     *
     * @return
     */
    public final Map<String, SysRegionalAgentDto> getAgentName2BeanMap() {
        Map<String, SysRegionalAgentDto> agentName2BeanMap = new HashMap();
        List<SysRegionalAgentDto> sysRegionalAgentDtoList = sysRegionalAgentService.list(null).getData();
        if (CollectionUtils.isNotEmpty(sysRegionalAgentDtoList)) {
            for (SysRegionalAgentDto sysRegionalAgentDto : sysRegionalAgentDtoList) {
                agentName2BeanMap.put(sysRegionalAgentDto.getName(), sysRegionalAgentDto);
            }
        }
        return agentName2BeanMap;
    }

    /**
     * 获取公司的集合信息 map对应的键值为 shortName:sysRegionalAgentDto
     *
     * @return
     */
    public final Map<String, SysCompanyDto> getCompanyName2BeanMap() {
        Map<String, SysCompanyDto> companyName2BeanMap = new HashMap();
        List<SysCompanyDto> sysCompanyDtoList = sysCompanyService.list(null).getData();
        if (CollectionUtils.isNotEmpty(sysCompanyDtoList)) {
            for (SysCompanyDto sysCompanyDto : sysCompanyDtoList) {
                companyName2BeanMap.put(sysCompanyDto.getShortName(), sysCompanyDto);
            }
        }
        return companyName2BeanMap;
    }

    /**
     * 获取车辆表中的所有车牌号
     *
     * @return
     */
    public final Set<String> getCarPlateNumSet() {
        Set<String> carPlateNumSet = new HashSet<>();
        List<CarDto> carDtoList = carService.list(null).getData();
        if (CollectionUtils.isNotEmpty(carDtoList)) {
            for (CarDto carDto : carDtoList) {
                carPlateNumSet.add(carDto.getPlateNum());
            }
        }
        return carPlateNumSet;
    }

    /**
     * 获取车辆表中的所有车牌号
     *
     * @return
     */
    public final Map<String, CarDto> getCarPlateNum2BeanMap() {
        Map<String, CarDto> carPlateNum2BeanMap = new HashMap<>();
        List<CarDto> carDtoList = carService.list(null).getData();
        if (CollectionUtils.isNotEmpty(carDtoList)) {
            for (CarDto carDto : carDtoList) {
                carPlateNum2BeanMap.put(carDto.getPlateNum(), carDto);
            }
        }
        return carPlateNum2BeanMap;
    }

    /**
     * 根据类型获取车辆表中的车牌号
     *
     * @return
     */
    public final Map<String, String> getCarPlateNumMapByType(Map<String, Object> paramsMap) {
        Map<String, String> carPlateNumMap = new HashMap<>();
        List<CarDto> carDtoList = carService.list(paramsMap).getData();
        if (CollectionUtils.isNotEmpty(carDtoList)) {
            for (CarDto carDto : carDtoList) {
                carPlateNumMap.put(carDto.getPlateNum(), carDto.getUuid());
            }
        }
        return carPlateNumMap;
    }

    /**
     * 获取司机表中的所有车辆uuid
     *
     * @return
     */
    public final Set<String> getDriverPlateNumSet() {
        Set<String> driverPlateNumSet = new HashSet<>();
        List<DriverDto> driverDtoList = driverService.list(null).getData();
        if (CollectionUtils.isNotEmpty(driverDtoList)) {
            for (DriverDto driverDto : driverDtoList) {
                driverPlateNumSet.add(driverDto.getCarUuid());
            }
        }
        return driverPlateNumSet;
    }

    /**
     * 获取司机表中的  mobile:driverDto
     *
     * @return
     */
    public final Map<String, DriverDto> getDriverMobile2BeanMap() {
        Map driverMobile2BeanMap = new HashMap();
        ResponseData<List<DriverDto>> ajaxList = driverService.list(null);
        if (ajaxList.isSuccess() && CollectionUtils.isNotEmpty(ajaxList.getData())) {
            List<DriverDto> driverDtoList = ajaxList.getData();
            for (DriverDto driverDto : driverDtoList) {
                // 排除掉离职的司机
                if (driverDto.getStatus() != DriverConstant.DRIVER_STATUS_DIMISSION) {
                    driverMobile2BeanMap.put(driverDto.getMobile(), driverDto);
                }
            }
        }
        return driverMobile2BeanMap;
    }

    /**
     * 获取司机表中的  idCard:driverDto
     *
     * @return
     */
    public final Map<String, DriverDto> getDriverIdCard2BeanMap() {
        Map driverIdCard2BeanMap = new HashMap();
        ResponseData<List<DriverDto>> ajaxList = driverService.list(null);
        if (ajaxList.isSuccess() && CollectionUtils.isNotEmpty(ajaxList.getData())) {
            for (DriverDto driverDto : ajaxList.getData()) {
                driverIdCard2BeanMap.put(driverDto.getIdCard(), driverDto);
            }
        }
        return driverIdCard2BeanMap;
    }

    /**
     * 获取司机表中的  mobile+idcard:uuid
     *
     * @return
     */
    public final Map<String, String> getDriverMobileAndIdCard2UuidMap() {
        Map driverMobileAndIdCard2UuidMap = new HashMap();
        ResponseData<List<DriverDto>> ajaxList = driverService.list(null);
        if (ajaxList.isSuccess() && CollectionUtils.isNotEmpty(ajaxList.getData())) {
            for (DriverDto driverDto : ajaxList.getData()) {
                driverMobileAndIdCard2UuidMap.put(driverDto.getMobile() + driverDto.getIdCard(), driverDto.getUuid());
            }
        }
        return driverMobileAndIdCard2UuidMap;
    }


    /**
     * 获取车辆表中的  plateNum:uuid
     *
     * @return
     */
    public final Map<String, String> getCarPlateNum2UuidMap() {
        Map driverMobile2UuidMap = new HashMap();
        List<CarDto> driverDtoList = carService.list(MapUtils.build()).getData();
        if (driverDtoList != null) {
            for (CarDto driverDto : driverDtoList) {
                driverMobile2UuidMap.put(driverDto.getPlateNum(), driverDto.getUuid());
            }
        }
        return driverMobile2UuidMap;
    }

    /**
     * 获取平台的开通城市中的  cityName:uuid
     *
     * @return
     */
    public final Map<String, String> getCityName2UuidMap(String appid) {
        Map cityName2UuidMap = new HashMap();
        List<SysCityDto> sysCityDtoList = sysCityService.getOpenCities(MapUtils.build("organizationUuid", appid)).getData();
        for (SysCityDto sysCityDto : sysCityDtoList) {
            cityName2UuidMap.put(sysCityDto.getCity(), sysCityDto.getUuid());
        }
        return cityName2UuidMap;
    }

    public Map<String, List<SysBusinessDto>> getOrgUuid_ListBusMap(String appid) {
        ResponseData<List<SysBusinessDto>> responseData = sysBusinessOrganizationService.getOrgUuidBusName2BeanMap(
                MapUtils.build("appid", appid));
        if (responseData.isSuccess() && CollectionUtils.isNotEmpty(responseData.getData())) {
            List<SysBusinessDto> list = responseData.getData();
            return list.stream().collect(Collectors.groupingBy(SysBusinessDto::getOrganizationUuid));
        }
        return new HashMap<>(2);
    }
}
